package com.neusoft.elmboot.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.neusoft.elmboot.common.ElmException;
import com.neusoft.elmboot.common.Pager;
import com.neusoft.elmboot.mapper.BaseMapper;
import com.neusoft.elmboot.service.BaseService;

/**
 * 默认的Service接口实现类，
 * 
 * @author chens
 *
 * @param <T>
 */

public class DefaultServiceImpl<T> implements BaseService<T> {

	@Autowired
	private BaseMapper<T> baseMapper;

	@Override
	public boolean add(T object) {
		boolean ret = false;
		try {
			ret = baseMapper.add(object);
		} catch (Exception e) {
			throw new ElmException("新增异常", e);
		}
		return ret;
	}

	@Override
	public boolean edit(T object) {

		boolean ret = false;
		try {
			ret = baseMapper.edit(object);
		} catch (Exception e) {
			throw new ElmException("修改异常", e);
		}
		return ret;
	}

	@Override
	public boolean deleteById(Integer id) {

		boolean ret = false;
		try {
			ret = baseMapper.deleteById(id);
		} catch (Exception e) {
			throw new ElmException("删除异常", e);
		}
		return ret;
	}

	@Override
	public T findById(Integer id) {
		T ret = null;

		try {
			ret = baseMapper.findById(id);
		} catch (Exception e) {
			throw new ElmException("查询异常", e);
		}
		return ret;
	}

	@Override
	public int findTotal(Pager<T> pager) {

		int ret = 0;

		try {
			ret = baseMapper.findTotal(pager);
		} catch (Exception e) {
			throw new ElmException("查询异常", e);
		}
		return ret;
	}

	@Override
	public List<T> findByPager(Pager<T> pager) {

		List<T> ret = new ArrayList<T>();

		try {
			ret = baseMapper.findByPager(pager);
		} catch (Exception e) {
			throw new ElmException("查询异常", e);
		}
		return ret;
	}

	@Override
	public boolean deleteByIds(List<Integer> ids) {

		boolean ret = false;
		try {
			ret = baseMapper.deleteByIds(ids);
		} catch (Exception e) {
			throw new ElmException("删除异常", e);
		}
		return ret;
	}

}
